package com.atguigu.springcloud.alibaba.service.impl;

import com.atguigu.springcloud.alibaba.dao.OrderDao;
import com.atguigu.springcloud.alibaba.domain.Order;
import com.atguigu.springcloud.alibaba.service.AccountService;
import com.atguigu.springcloud.alibaba.service.OrderService;
import com.atguigu.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xiaolong
 * @version 1.0
 * @description: TODO
 * @date 2021/10/10 14:34
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * @param
     * @return
     * @description: 创建订单-》调用库存服务扣减库存-》调用账户服务扣减账户余额-》修改订单状态
     */
    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)  //所有异常都回滚，name随便取，但不能重复
    public void create(Order order) {

        //1.新键订单
        log.info("----->开始新键订单");
        orderDao.create(order);

        //2.扣减库存
        log.info("----->订单微服务开始调用库存，做扣减");
        storageService.decrease(order.getProductId(), order.getCount());

        //3.扣减账户余额
        log.info("----->订单微服务开始调用账户，做扣减");
        accountService.decrease(order.getUserId(), order.getMoney());

        //4.修改订单状态，从0到1，说明1代表订单完成
        log.info("修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("修改订单状态结束");

        log.info("-------->下订单结束了， 哈哈哈");
    }
}
